package com.ruoyi;

import com.alibaba.fastjson.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class DingSend {
    final static int cores = Runtime.getRuntime().availableProcessors() + 1;

    final static ExecutorService exec = new ThreadPoolExecutor(cores+10, cores+50, 60, TimeUnit.SECONDS,
            new ArrayBlockingQueue<Runnable>(500));
    public static void main(String[] args) {
        try {
            exec.submit(new Runnable() {
                @Override
                public void run() {
                    try {
                        dingding("36735439c882d614c8c5c3eae039d8885ea28c709a947a88a35f9a8de7e96068","预算test");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     *
     * @param token 群机器人令牌，每个群添加的机器人令牌不同
     * @param content 推送内容，需带上配置的安全关键字
     * @return
     * @throws Exception
     */
    public static String dingding(String token, String content) throws Exception {
        PrintWriter out = null;
        URL serverUrl = new URL("https://oapi.dingtalk.com/robot/send?access_token=" + token);
        HttpURLConnection conn = (HttpURLConnection) serverUrl.openConnection();
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setDoInput(true);
        conn.setConnectTimeout(3000);
        conn.setReadTimeout(3000);
        conn.setDoOutput(true);
        Map<String, Object> date = new HashMap<>();
        date.put("msgtype", "text");
        Map<String, Object> text = new HashMap<>();
        text.put("content", content);
        date.put("text", text);

        // 获取URLConnection对象对应的输出流
        out = new PrintWriter(conn.getOutputStream());
        // 发送请求参数
        JSONObject json = new JSONObject(date);
        out.print(json.toJSONString());
        // flush输出流的缓冲
        out.flush();
        conn.setInstanceFollowRedirects(false);
        conn.connect();
        return getResult(conn.getInputStream());
    }

    private static String getResult(InputStream inputStream) {
        String result = "";
        ByteArrayOutputStream dataOutputStream = null;
        try {
            byte[] buf = new byte[1024];
            int n;
            dataOutputStream = new ByteArrayOutputStream();

            while (((n = inputStream.read(buf)) != -1)) {
                dataOutputStream.write(buf, 0, n);
            }
            dataOutputStream.toByteArray();
            result = new String(dataOutputStream.toByteArray(), "UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }


}
